jQuery(function() {
    jQuery("a.create").click(function() {
        var params = {};
        params.parentId = PageContext.getAttribute("categoryId");
        params.name = "新建分类";

        jQuery.ajax({
            "type": "post",
            "url": PageContext.getContextPath() + "/note/category/create.html",
            "data": jQuery.param(params, true),
            "dataType": "json",
            "error": function(){
                alert("系统错误，请稍后再试！");
            },
            "success": function(result){
                Response.success(result, function(){
                    window.location.reload();
                });
            }
        });
    });

    jQuery("table tr td input, table tr td select").change(function() {
        var tr = jQuery(this).closest("tr");
        var categoryId = tr.attr("categoryId");
        var name = this.name;
        var value = this.value;
        var params = {};
        params.categoryId = categoryId;
        params[name] = value;

        jQuery.ajax({
            "type": "post",
            "url": PageContext.getContextPath() + "/note/category/setProperties.html",
            "data": jQuery.param(params, true),
            "dataType": "json",
            "error": function(){
            },
            "success": function(returnValue){
            }
        });
    });
});

jQuery(function() {
    var adjust = function() {
        jQuery("#category-list tr").each(function() {
            var tr = this;
            var rowIndex = tr.rowIndex;
            var categoryId = tr.getAttribute("categoryId");
            var orderNo = tr.getAttribute("orderNo");

            if(categoryId == null) {
                return;
            }

            if(orderNo != (rowIndex).toString()) {
                jQuery.ajax({
                    "type": "post",
                    "url": PageContext.getContextPath() + "/note/category/setProperties.html",
                    "data": {"categoryId": categoryId, "orderNo": rowIndex},
                    "dataType": "json",
                    "error": function() {
                    },
                    "success": function(returnValue) {
                        tr.setAttribute("orderNo", rowIndex);
                    }
                });
            }
        });
    };

    jQuery("#category-list tr").click(function() {
        if(this.rowIndex < 1) {
            return;
        }
        jQuery("#category-list tr").removeClass("selected");
        jQuery(this).addClass("selected");
    });

    jQuery("a.button.moveup").click(function() {
        var e = jQuery("#category-list tr.selected");

        if(e.size() < 1) {
            return;
        }

        var tr = e.get(0);
        var categoryId = tr.getAttribute("categoryId");

        if(categoryId == null) {
            return;
        }

        if(tr.rowIndex > 1) {
            TableUtil.moveup(tr);
            adjust();
        }
    });

    jQuery("a.button.movedown").click(function() {
        var e = jQuery("#category-list tr.selected");

        if(e.size() < 1) {
            return;
        }

        var tr = e.get(0);
        var categoryId = tr.getAttribute("categoryId");

        if(categoryId == null) {
            return;
        }

        var rowIndex = tr.rowIndex;
        TableUtil.movedown(tr);
        var newIndex = tr.rowIndex;

        if(rowIndex != newIndex) {
            adjust();
        }
    });

    jQuery("a.bk-btn-delete").click(function() {
        var categoryId = jQuery(this).attr("categoryId");

        jQuery.ajax({
            "type": "post",
            "url": PageContext.getContextPath() + "/note/category/delete.html",
            "data": {"categoryId": categoryId},
            "dataType": "json",
            "error": function(){
            },
            "success": function(returnValue) {
                window.location.reload();
            }
        });
    });
});
